package com.inei.eea2014.controller.activity;

import java.util.List;

import com.inei.eea2014.R;
import com.inei.eea2014.controller.CaratulaController;
import com.inei.eea2014.controller.OpcionesController;
import com.inei.eea2014.model.entity.Caratula;
import com.inei.eea2014.model.entity.Ciiu;
import com.inei.eea2014.tools.Mensaje;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class Caratula_Fragment extends Fragment {

	// componentes
	private View root;
	private EditText etC_1, etC_2, etC_3, etC_4, etC_5;
	private Spinner spC_6;

	// variables
	private String RUC;
	private String USUARIO;
	private CaratulaController carCon;
	private OpcionesController opcCon;
	private List<Ciiu> lCiiu;
	private Caratula obj;
	private Mensaje m;
	private boolean nuevo = false;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		root = inflater.inflate(R.layout.fragment_caratula, container, false);

		return root;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		carCon = new CaratulaController(getActivity());
		opcCon = new OpcionesController(getActivity());
		m = new Mensaje(getActivity());

		IniciarComponentes();

		AgregarEventos();

		CargarDatos();
	}

	private void IniciarComponentes() {
		etC_1 = (EditText) root.findViewById(R.id.etC_1);
		etC_2 = (EditText) root.findViewById(R.id.etC_2);
		etC_3 = (EditText) root.findViewById(R.id.etC_3);
		etC_4 = (EditText) root.findViewById(R.id.etC_4);
		etC_5 = (EditText) root.findViewById(R.id.etC_5);

		spC_6 = (Spinner) root.findViewById(R.id.spC_6);

		// Spinner Ciiu
		lCiiu = opcCon.CiiuObtenerTodo();
		Ciiu ciiu = new Ciiu("0000", "Seleccionar CIIU");
		lCiiu.add(0, ciiu);
		spC_6.setAdapter(new ArrayAdapter<Ciiu>(getActivity(),
				android.R.layout.simple_spinner_item, lCiiu));
	}

	private void AgregarEventos() {
		// TODO Auto-generated method stub

	}

	private void CargarDatos() {
		RUC = getArguments().getString("RUC");
		USUARIO = getArguments().getString("USUARIO");

		obj = new Caratula(RUC, USUARIO);

		obj = carCon.CarObtener(obj);
		if (obj != null) {
			etC_1.setText(obj.getC0000_001_01_T());
			etC_2.setText(obj.getC0000_001_02_T());
			etC_3.setText(obj.getC0000_001_03_T());
			etC_4.setText(obj.getC0000_001_04_T());
			etC_5.setText(obj.getC0000_001_05_T());

			if (obj.getC0000_001_06_T() != null) {
				for (int i = 0; i < lCiiu.size(); i++) {
					if (obj.getC0000_001_06_T().equals(lCiiu.get(i).getCIIU())) {
						spC_6.setSelection(i);
						break;
					}
				}
			}
		}
	}

	public void ValidarCap() {
		// TODO Auto-generated method stub

	}

	public void GuardarCap() {
		if (obj == null) {
			nuevo = true;
			obj = new Caratula(RUC, USUARIO);
		}

		obj.setC0000_001_01_T(etC_1.getText().toString());
		obj.setC0000_001_02_T(etC_2.getText().toString());
		obj.setC0000_001_03_T(etC_3.getText().toString());
		obj.setC0000_001_04_T(etC_4.getText().toString());
		obj.setC0000_001_05_T(etC_5.getText().toString());
		obj.setC0000_001_06_T(((Ciiu) spC_6.getSelectedItem()).getCIIU());

		if (nuevo) {
			if (carCon.CarInsertar(obj) > 0) {
				nuevo = false;
				// ValidarJust();
				m.Exito("SE GRABO CON EXITO");
			} else {
				m.Error("ERROR AL GRABAR");
			}
		} else {
			if (carCon.CarModificar(obj) > 0) {
				// ValidarJust();
				m.Exito("SE MODIFICO CON EXITO");
			} else {
				m.Error("ERROR AL MODIFICAR");
			}
		}
	}
}
